#include "SearchMenu.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "MenuManager.h"
#include "ServiceProvider.h"
#include "PeopleManager.h"
#include "PlayerMenu.h"
#include "People.h"
#include "TeamMenu.h"
#include "Team.h"
#include "TeamManager.h"

#include "StlStringAndStreams.h"

#include <cassert>

SearchMenu::SearchMenu(MenuManager* const i_menuManager)
	:Menu(i_menuManager)
{
}

void SearchMenu::Draw() const
{
	using STD::cout; using STD::endl;
	using Utilities::TabUpTo; using Utilities::IntToStlString;

	cout << Print::Title("SEARCH MENU:");

	cout << "RESULTS" << endl << endl;

	STL::string options;
	if(m_playersFind.empty() && m_teamsFind.empty())
	{
		cout << TabUpTo("-",5);
		cout << TabUpTo("no result",People::GetParams().MAX_CHAR_NAME) << endl << endl;

		options = "Type something for a new search.\n\n0) Return to previous menu'.\n";
	}
	else
	{
		size_t i = 0;
		for(; i <= m_playersFind.size(); ++i)
		{
			if(i == 0)
				cout << "Busy Players" << endl;
			if(i == m_lastBusy)
				cout << endl << "Free Players" << endl;
			if(i== m_lastFree)
				cout << endl << "Busy Trainers" << endl;
			if(i == m_lastBusyTrainer)
				cout << endl << "Free Trainers" << endl;
			if(i == m_lastFreeTrainer)
				cout << endl << "Hall Of Fame" << endl;
			if(i != m_playersFind.size())
			{
				cout << TabUpTo(i+1,5);
				cout << TabUpTo(m_playersFind[i]->GetName(),People::GetParams().MAX_CHAR_NAME) << endl;
			}
		}
		cout << endl << "Teams" << endl;

		size_t j=0;
		for(; j < m_teamsFind.size(); ++j)
		{
			cout << TabUpTo(j+i,5);
			cout << TabUpTo(m_teamsFind[j]->GetName(),Team::GetParams().MAX_CHAR_TEAM_NAME) << endl;
		}

		cout << endl << endl;

		options = "Insert player name for a new search.\n";
		options += (!m_playersFind.empty() ? ("1-" + IntToStlString(m_playersFind.size()) + ") Info player \n") : (""));
		options += (!m_teamsFind.empty() ? (IntToStlString(m_playersFind.size()+1) + "-" + IntToStlString(m_playersFind.size() + m_teamsFind.size()) + ") Info Team\n") : (""));
		options += "\n0) Return to previous menu'.\n";
	}

	cout << Print::Options(options) << "Selection: ";
}

bool SearchMenu::Input(const STL::string& i_input)
{
	
	unsigned int res = Utilities::StringToInt(i_input);

	if (res == 0)
	{
		m_playersFind.clear();
		GetMM()->SetMenu(GetMM()->GetMainMenu());
	}
	else if(res <= m_playersFind.size())
	{
		PlayerMenu* nextMenu = static_cast<PlayerMenu*>(GetMM()->GetPlayerMenu());
		nextMenu->SetPlayer(GetSP()->GetPeopleMgr().GetPeopleFromID(m_playersFind[res-1]->GetId()));
		GetMM()->PushMenu(nextMenu);
		return true;
	}
	else if(res <= m_playersFind.size() + m_teamsFind.size())
	{
		TeamMenu* nextMenu = static_cast<TeamMenu*>(GetMM()->GetTeamMenu());
		nextMenu->SetTeamID(m_teamsFind[res-m_playersFind.size()-1]->GetID());
		GetMM()->PushMenu(nextMenu);
		return true;
	}
	else
	{
		m_playersFind.clear();
		m_teamsFind.clear();

		STL::string input(i_input);
		ToLowerString(input);

		const PeopleManager& PM = GetSP()->GetPeopleMgr();
		m_lastBusy = FillVector(PM.GetBusyVec(true), input);
		m_lastFree = FillVector(PM.GetFreeVec(true), input);
		m_lastBusyTrainer = FillVector(PM.GetBusyVec(false), input);
		m_lastFreeTrainer = FillVector(PM.GetFreeVec(false), input);
		FillVector(PM.GetHofVec(), input);

		const TeamManager& TM = GetSP()->GetTeamMgr();
		const TeamVec& teams = TM.GetTeams();
		STL::string temp;
		for(size_t i = 0;  i < teams.size(); ++i)
		{
			temp = teams[i]->GetName();
			ToLowerString(temp);
			if(temp.find(input) <= temp.size())
				m_teamsFind.push_back(teams[i]);
		}
	}
	
	return false;
}

size_t SearchMenu::FillVector(const PeopleVec& i_vec, const STL::string& i_search)
{
	STL::string temp;
	for(size_t i = 0;  i < i_vec.size(); ++i)
	{		
		temp = i_vec[i]->GetName();
		ToLowerString(temp);
		if(temp.find(i_search) <= temp.size())
			m_playersFind.push_back(i_vec[i]);
	}
	return m_playersFind.size();
}

